/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.aurelius;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

/**
 *
 */
public class Dao {

    private EntityManagerFactory emf;

    @PersistenceUnit
    public void setEntityManagerFactory(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public void persistSimplePojo(SimplePojo simplePojo) {
        EntityManager em = this.emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(simplePojo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SimplePojo> getAllPojos() {
        EntityManager em = this.emf.createEntityManager();
        try {
            Query q = em.createQuery("from SimplePojo");
            return q.getResultList();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public SimplePojo getPojoById(Integer id) {
        EntityManager em = this.emf.createEntityManager();
        SimplePojo instance = em.find(SimplePojo.class, id);
        return instance;
    }
}
